package com.beagle.njddapp.weight.dialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.dirver.OilOrderState;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.core.UserService;
import com.blankj.utilcode.util.ToastUtils;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 */
public class DIrverDIalog extends Dialog {


    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_owner)
    TextView tvOwner;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_caozuo)
    TextView tvCz;
    @BindView(R.id.rl_worktype)
    RelativeLayout rlWorkType;
    @BindView(R.id.tv_worktype)
    TextView tvWorkType;
    @BindView(R.id.rl_endtime)
    RelativeLayout rlendtime;
    @BindView(R.id.tv_endtime)
    TextView tvendTime;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_owner_phone)
    TextView tvPhone;
    @BindView(R.id.tv_tz)
    TextView tcTz;
    @BindView(R.id.iv_next)
    ImageView ivNext;
    @BindView(R.id.rl_resean)
    RelativeLayout rlReason;
    @BindView(R.id.ed_reason)
    EditText edReason;
    @BindView(R.id.rl_owner)
    RelativeLayout rlOwner;
    private String lastStr;
    private String lastTime="";
    private DirverJiaYouCallBack dirverJiaYouCallBack;
    private DIrverOrderBean dIrverOrderBean;
    public void setDirverJiaYouCallBack(DirverJiaYouCallBack dirverJiaYouCallBack) {
        this.dirverJiaYouCallBack = dirverJiaYouCallBack;
    }

    public void update(DIrverOrderBean orderBean_out, OilOrderState o){
        this.dIrverOrderBean=orderBean_out;
        tvType.setText(orderBean_out.getMeshTypeName());
        tvOwner.setText(orderBean_out.getMeshUserName());
        tvPhone.setText(orderBean_out.getMeshUserPhone());
        tvNumber.setText(orderBean_out.getMeshNum());
        tvWorkType.setText(orderBean_out.getWorkTypeName());
        tvendTime.setText(orderBean_out.getEndDate());
        rlOwner.setVisibility(View.VISIBLE);
        rlReason.setVisibility(View.GONE);
        tcTz.setText("调整");
        tvCz.setText(lastStr);
        ivNext.setVisibility(View.GONE);
        tvBack.setText("归还设备");
        if (orderBean_out.getOrderStatus()==5){
            tvBack.setVisibility(View.GONE);
            tvCz.setText("归还确认中");
            tcTz.setText("");
            tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_dd));
            return;
        }
        if (o==null||o.getLastOilRequest()==null){
            tvCz.setText("申请加油");
            tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_blue));
            return;
        }
        if (o.getLastOilRequest()!=null){
            OilOrderState.LastOilRequestBean lastOilRequestBean=o.getLastOilRequest();
            if ("submit".equals(lastOilRequestBean.getRequestState())){
                tvCz.setText("加油申请待农机主确认");
                tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_dd));
                return;
            }
            if ("refuse".equals(lastOilRequestBean.getRequestState())){
                tvCz.setText("申请加油");
                tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_blue));
                return;
            }
            if ("accept".equals(lastOilRequestBean.getRequestState())){
                if ("waiting".equals(lastOilRequestBean.getOrderState())){
                    tvCz.setText("等待接单中");
                    tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_dd));
                }else if ("confirmed".equals(lastOilRequestBean.getOrderState())){
                    tvCz.setText("等待加油车到来");
                    tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_dd));
                }else if ("arrived".equals(lastOilRequestBean.getOrderState())){
                    tvCz.setText("加油车已到达");
                    tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_dd));
                }else {
                    tvCz.setText("申请加油");
                    tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_blue));
                }
            }else {
                tvCz.setText("申请加油");
                tvCz.setBackground(getContext().getResources().getDrawable(R.drawable.bg_radius_4_blue));
            }
        }

    }
    public DIrverDIalog(@NonNull Context context) {
        super(context);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dirver);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);
        }
    }

    @OnClick({R.id.container,R.id.tv_caozuo, R.id.tv_back, R.id.tv_tz, R.id.iv_next})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.container:
                dismiss();
                break;
            case R.id.tv_caozuo:
                if ("申请加油".equals(tvCz.getText().toString())){
                    if (dirverJiaYouCallBack!=null){
                        dirverJiaYouCallBack.jiayou();
                    }
                    dismiss();
                }else if ("确认提交".equals(tvCz.getText().toString())){
                    if (TextUtils.isEmpty(edReason.getText().toString())){
                        ToastUtils.showShort("请输入原因");
                        return;
                    }
                    if (lastTime.equals(tvendTime.getText().toString())){
                        ToastUtils.showShort("当前时间和修改前的时间相同");
                        return;
                    }
                    dismiss();
                    tcTz.setText("调整");
                    ivNext.setVisibility(View.GONE);
                    tvBack.setText("归还设备");
                    if (dirverJiaYouCallBack!=null){
                        dirverJiaYouCallBack.tz(edReason.getText().toString(),dIrverOrderBean.getOrderId(),tvendTime.getText().toString());
                    }
                }

                break;
            case R.id.tv_back:
                if ("取消调整".equals(tvBack.getText().toString())) {
                    rlOwner.setVisibility(View.VISIBLE);
                    rlReason.setVisibility(View.GONE);
                    tcTz.setText("调整");
                    tvCz.setText(lastStr);
                    tvendTime.setText(dIrverOrderBean.getEndDate());
                    ivNext.setVisibility(View.GONE);
                    tvBack.setText("归还设备");
                } else {
                    if ("申请加油".equals(tvCz.getText().toString())){
                        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                        builder.setTitle("是否确定归还");
                        //点击对话框以外的区域是否让对话框消失
                        builder.setCancelable(true);
                        //设置正面按钮
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (dirverJiaYouCallBack!=null){
                                    dirverJiaYouCallBack.giveBack(dIrverOrderBean.getOrderId());
                                }
                                DIrverDIalog.this.dismiss();
//                            presenter.deviceGiveBack(data!=null?data.getOrderId():orderBean.getOrderId());

                            }
                        });
                        //设置反面按钮
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();

                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }else {
                        ToastUtils.showShort("当前有未完成的加油订单，暂无法归还");
                    }

                }

                break;
            case R.id.iv_next:
                chooseTime();
                break;
            case R.id.tv_tz:
                rlOwner.setVisibility(View.GONE);
                rlReason.setVisibility(View.VISIBLE);
                ivNext.setVisibility(View.VISIBLE);
                edReason.setText("");
                tcTz.setText("");
                tvBack.setText("取消调整");
                lastStr=tvCz.getText().toString();
                lastTime=tvendTime.getText().toString();
                tvCz.setText("确认提交");
                break;
        }
    }
    private void chooseTime() {
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog datePickerDialog= new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                String monthStr;
                if (month < 9) {
                    monthStr = "0" + (month + 1);
                } else {
                    monthStr = (month + 1) + "";
                }

                tvendTime.setText(year + "-" + monthStr + "-" + dayOfMonth);

            }
        },

                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMinDate(new Date().getTime());
        datePickerDialog.show();

    }
    public interface  DirverJiaYouCallBack{
        void jiayou();
        void tz(String reason,int id,String time);
        void giveBack(int id);
    }



}
